# -*- coding: utf-8 -*-


__author__ = "Ypf"
_date_ = "2019/6/20  13:41"

'''
客户经理相关数据报表模块
'''

from common.utils import format_float, level_charge
from crm_data_report.config  import crm_cal_fields, jingli_list, jlcc_dic, workday, kehujili_general_list
from crm_data_report.cm_db import AccountMgr, filter_sb
def chuchai(dic):
    for key in dic:
        if key in jlcc_dic:
            team_xs = workday / (workday - jlcc_dic[key]) * dic[key][0]
            dic[key][0]=team_xs
    return dic

def convert_bili(input_dic, bizhi):
    #计算所占据的比例
    zongfen = []
    for val in input_dic.values():
        zongfen.append(val[0])
    zongfen = sum(zongfen)
    for key in input_dic:
        val = input_dic[key]
        fir_num = format_float(val[0])
        bl_num = fir_num / zongfen * bizhi
        input_dic[key] = [fir_num, format_float(bl_num)]
    return input_dic

# 将客户绩效合并到销售积分并且算出后续数据
def hebing_dic(dic1, dic2):
    #防止客户绩效和客户积分合并时候出现不吻合情况以及为零情况
    for sanme_key in dic1:
        if sanme_key in dic2:
            for i in dic2[sanme_key]:
                dic1[sanme_key].append(i)
        else:
            dic1[sanme_key].extend([0, 0])
        #二项积分合计
        exjfhj_five_num = dic1[sanme_key][1] + dic1[sanme_key][3]
        dic1[sanme_key].append(format_float(exjfhj_five_num))
        #积分合计/平均分
        jfhj_pjf = dic1[sanme_key][4] / 100 * len(dic1)
        jfhj_pjf = format_float(jfhj_pjf)
        dic1[sanme_key].append(format_float(jfhj_pjf))
        # 等级绩效
        level=level_charge(jfhj_pjf)
        for i in  level:
            dic1[sanme_key].append(i)
        try:
            dic1[sanme_key].append(jlcc_dic[sanme_key])
            if jlcc_dic[sanme_key]!=0:
                dic1[sanme_key].append(dic1[sanme_key][0])
            else:
                dic1[sanme_key].append(0)
        except:
            dic1[sanme_key] = tuple(dic1[sanme_key])
    return dic1
def filter_dic(account_dict_all,khjx_dic_all,list):
    # 根据不同的列表来过滤出两个表格
    compare_dic = filter_sb(account_dict_all, list)
    # 算出所对应的比例值
    dic_compare_xsjf = convert_bili(compare_dic, 70)
    # 过滤生成客户绩效字典
    com_khjx = filter_sb(khjx_dic_all, list)
    # 算出所对应的比例值
    dic_compare_khjx = convert_bili(com_khjx, 30)
    # 合并生成后续数据
    end_compare_dic = hebing_dic(dic_compare_xsjf, dic_compare_khjx)
    return end_compare_dic
def  cm_main(begin_day,end_day):
    #时间的调节准备
    year = begin_day.split("-")[0]
    month = begin_day.split("-")[1]
    begin_time = f"{begin_day} 00:00:00"
    end_time = f"{end_day} 23:59:59"
    mohu_time = f"{year}-{month[-1]}%" if month.startswith('0') else f"{year}-{month}%"
    cal_type = crm_cal_fields['account_rp']

    #获取相应的id-name  表格
    id_sql = cal_type.get('id_sname')
    name_dic = AccountMgr.get_id_name(id_sql)



    #所有客户经理对比表
    #输入时间生成销售积分字典
    sales_score_sql = cal_type.get('xsjf_score') % (begin_time, end_time)
    account_dict_all = AccountMgr.get_account_groupby(sales_score_sql, name_dic)
    account_dict_all=chuchai(account_dict_all)
    #算出所对应的比例值
    dic_all = convert_bili(account_dict_all, 70)
    # 输入时间生成销售积分字典
    khjx_sql = cal_type.get('kehu_jixiao') % (mohu_time)
    khjx_dic_all = AccountMgr.get_kehujixiao(khjx_sql, name_dic)
    # 算出所对应的比例值
    dic2_all = convert_bili(khjx_dic_all, 30)
    #合并生成后续数据已字典的形式存储
    end_all_dic=hebing_dic(dic_all, dic2_all)
    conpare_general=filter_dic(account_dict_all,khjx_dic_all,kehujili_general_list)

    # 存入sql
    db_insert_general_sql = crm_cal_fields['account_rp']['dbkhjlquanbu_insert_sql']
    compare_khjl=filter_dic(account_dict_all,khjx_dic_all,jingli_list)


    #另外对比的表
    # 过滤生成销售积分字典

    # 存入sql
    db_insert_compare_sql = crm_cal_fields['account_rp']['dbkhjldandu_insert_sql']

#     存入数据库
    AccountMgr.store_khjlqb_statics(begin_day, end_day, conpare_general, db_insert_general_sql)
    AccountMgr.store_khjldd_statics(begin_day,end_day,compare_khjl,db_insert_compare_sql)
    return  end_all_dic



